package com.example.maple.ccsclient.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.adapter.GridViewAdapter;
import com.example.maple.ccsclient.instance.DataInfoInstance;
import com.example.maple.ccsclient.interfaces.IPreviewView;
import com.example.maple.ccsclient.model.DataModel;
import com.example.maple.ccsclient.model.PicModel;
import com.example.maple.ccsclient.presenter.HomePagePresenter;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;
import com.example.maple.ccsclient.view.MyGridView;
import com.example.maple.ccsclient.view.ObservableScrollView;
import com.example.maple.ccsclient.view.ScrollViewListener;
import com.facebook.drawee.backends.pipeline.Fresco;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements ScrollViewListener, IPreviewView {


	MyGridView mGridView = null;
	RelativeLayout image = null;
	//刷新按钮
	ImageButton mImageButton = null;
	ImageButton more = null;
	/**
	 * 自定义的MyScrollView
	 */
	private ObservableScrollView myScrollView;
	/**
	 * 在MyScrollView里面的布局
	 */
	private LinearLayout mViewTop;
	/**
	 * 位于顶部的布局
	 */
	private LinearLayout mView;

	//默认GridView一列
	int mColumn;
	Animation anim = null;

	//名言诗句
	TextView famousOne, famousTow;


	//
	List<PicModel> picList = null;
	GridViewAdapter mGridViewAdapter = null;


	HomePagePresenter mHomePagePresenter = null;
	private Context mContext;


	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Fresco.initialize(this);
		setContentView(R.layout.activity_main);
		mColumn = SharedPreferencesUtils.getPicColumn(this);
		mHomePagePresenter = new HomePagePresenter(this, this);
		mContext = this;
		findView();


	}

	@TargetApi(Build.VERSION_CODES.M)
	private void findView() {


		image = (RelativeLayout) findViewById(R.id.image);
		more = (ImageButton) findViewById(R.id.more);
		famousOne = (TextView) findViewById(R.id.famousOne);
		famousTow = (TextView) findViewById(R.id.famousTwo);
		mImageButton = (ImageButton) findViewById(R.id.refresh);
		anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.refresh_rotate);
		mImageButton.startAnimation(anim);
		anim.cancel();
		mImageButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//刷新数据
				mHomePagePresenter.refresh();
			}
		});
		more.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mHomePagePresenter.windowIsShowing()) {
					mHomePagePresenter.hiddenWindows();
				} else {
					//标题栏的高度
					mHomePagePresenter.showWindow(image, image.getWidth(), (int) image.getY());
				}

			}
		});
		myScrollView = (ObservableScrollView) findViewById(R.id.scrollView);
		mView = (LinearLayout) findViewById(R.id.searchView);
		mViewTop = (LinearLayout) findViewById(R.id.searchViewTop);
		mView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				InputActivity.startAction(MainActivity.this, "文字输入");
			}
		});
		mViewTop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				InputActivity.startAction(MainActivity.this, "文字输入");
			}
		});
		myScrollView.setScrollViewListener(this);

		//当布局的状态或者控件的可见性发生改变回调的接口
		findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				//这一步很重要，使得上面的编辑布局和下面的编辑布局重合
				onScrollChanged(null, 0, myScrollView.getScrollY(), 0, 0);
				System.out.println(myScrollView.getScrollY());
			}
		});


		picList = new ArrayList<>();
		mGridViewAdapter = new GridViewAdapter(this, R.layout.iamge_item, picList, mColumn);
		mGridView = (MyGridView) findViewById(R.id.gridView);
		mGridView.setNumColumns(mColumn);
		mGridView.setAdapter(mGridViewAdapter);
		//失去焦点可以放外层父控件监听套有 GridView ScrollView的页面布局 默认 起始位置不是最顶部。
		//	mGridView.setFocusable(false);
		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				PicModel info = (PicModel) parent.getItemAtPosition(position);
				DataInfoInstance.getInstance().setPicModels(picList);
				PicDetailActivity.startAction(mContext,position);
			}
		});

		//mGridView.setBackgroundColor(Color.argb(255,255,0,0));
		//	Utility.setListViewHeightBasedOnChildren(mListView);
		//获取数据
		mHomePagePresenter.refresh();

	}


	//获取滑动的距离，并且根据距离渐变
	@Override
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {

		int mBuyLayout2ParentTop = Math.max(y, mView.getTop());
		mViewTop.layout(0, mBuyLayout2ParentTop, mViewTop.getWidth(), mBuyLayout2ParentTop + mViewTop.getHeight());
	}

	//监听滑动状态，通过以此通知是否加载图片
	@Override
	public void onScrollStateChanged(ObservableScrollView view, int scrollState) {
		// 设置是否滚动的状态
		if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { // 不滚动状态
			mGridViewAdapter.setScrollState(false);
		} else {
			mGridViewAdapter.setScrollState(true);

		}
	}

	//显示名言诗句
	@Override
	public void showFamousVerse(String famousVerseOne, String famousVerseTwo) {
		famousOne.setText(famousVerseOne);
		famousTow.setText(famousVerseTwo);

	}

	//显示图片
	@Override
	public void showFamousVersePic(List<PicModel> list) {

		//因为是首页，所以无论是刷新还是第一次加载，都得把原来集合清理掉
		picList.clear();
		picList.addAll(list);
		mGridViewAdapter.notifyDataSetChanged();

	}

	//开始动画
	@Override
	public void startAnimation() {
		if (null != anim) {
			anim.start();
		}

	}

	//停止动画
	@Override
	public void stopAnimation() {
		if (null != anim) {
			anim.cancel();
		}
	}

	//显示提示框
	@Override
	public void showDialog(String toastText) {
		Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
	}

	@Override
	public String getFamousVerse() {
		return famousOne.getText().toString() + "。" + famousTow.getText().toString();
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (null != anim) {
			anim.cancel();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (null != anim) {
			anim.cancel();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != anim) {
			anim.cancel();
		}
	}
}
